package com.up.cloudfeiyibackend.service.impl;

import com.up.cloudfeiyibackend.constant.UserMessageConstant;
import com.up.cloudfeiyibackend.dto.UserPasswordDto;
import com.up.cloudfeiyibackend.dto.UserUpdateDto;
import com.up.cloudfeiyibackend.dto.UserLoginDTO;
import com.up.cloudfeiyibackend.entity.User;
import com.up.cloudfeiyibackend.exception.PasswordErrorException;
import com.up.cloudfeiyibackend.exception.UserExistsException;
import com.up.cloudfeiyibackend.exception.UserNotExistsException;
import com.up.cloudfeiyibackend.mapper.UserMapper;
import com.up.cloudfeiyibackend.properties.JwtProperties;
import com.up.cloudfeiyibackend.service.UserService;
import com.up.cloudfeiyibackend.util.JwtUtil;
import com.up.cloudfeiyibackend.util.UserHolder;
import com.up.cloudfeiyibackend.vo.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final JwtProperties jwtProperties;

    /**
     * 用户注册
     *
     * @param userLoginDTO
     */
    @Override
    public void register(UserLoginDTO userLoginDTO) {
        if (userMapper.findByUsername(userLoginDTO.getUsername()) != null){
            throw new UserExistsException(UserMessageConstant.USER_EXISTS);
        }
        User user = new User();
        user.setUsername(userLoginDTO.getUsername());
        user.setPassword(DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes()));

        userMapper.save(user);
    }

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @return
     */
    @Override
    public String login(UserLoginDTO userLoginDTO) {
        User user = userMapper.findByUsername(userLoginDTO.getUsername());
        if (user== null){
            throw new UserNotExistsException(UserMessageConstant.USER_NOT_EXISTS);
        }
        if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes()))){
            throw new PasswordErrorException(UserMessageConstant.PASSWORD_ERROR);
        }

        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        claims.put("username", user.getUsername());

        return JwtUtil.createJwt(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @Override
    public UserVo getCurrentUserInfo() {
        Long userId = UserHolder.get();
        User user = userMapper.findById(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    /**
     * 修改当前用户信息
     *
     * @param userUpdateDto
     * @return
     */
    @Override
    public UserVo updateCurrentUserInfo(UserUpdateDto userUpdateDto) {
        Long userId = UserHolder.get();
        User user = userMapper.findById(userId);
        user.setPhone(userUpdateDto.getPhone());
        user.setNickname(userUpdateDto.getNickname());
        user.setAvatarUrl(userUpdateDto.getAvatarUrl());

        userMapper.updateUserInfo(user);

        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    /**
     * 修改当前用户密码
     *
     * @param userPasswordDto
     */
    @Override
    public void updatePassword(UserPasswordDto userPasswordDto) {
        Long userId = UserHolder.get();
        User user = userMapper.findById(userId);

        String oldPassword = DigestUtils.md5DigestAsHex(userPasswordDto.getOldPassword().getBytes());
        if (!user.getPassword().equals(oldPassword)){
            throw new PasswordErrorException(UserMessageConstant.PASSWORD_ERROR);
        }
        user.setPassword(DigestUtils.md5DigestAsHex(userPasswordDto.getNewPassword().getBytes()));

        userMapper.updateUserPassword(user);
    }
}
